package com.qfjy.homework.student.yg.prox.handler;

import com.qfjy.homework.student.yg.prox.transaction.DaoTransaction;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * jdk代理
 */
public class TransactionHandler implements InvocationHandler {
    //增强类对象
    private DaoTransaction daoTransaction;
    //代理对象
    private Object object;

    public TransactionHandler(DaoTransaction daoTransaction, Object object) {
        this.daoTransaction = daoTransaction;
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object ret = null;

        //判断当前方法是否为addStudent
        if ("addStudent".equals(method.getName())){
            daoTransaction.before();
            ret = method.invoke(object, args);
            daoTransaction.after();
        }else {
            method.invoke(object,args);
        }

        return ret;
    }
}
